package com.ruoyi.member.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.AddressInfo;
import com.ruoyi.db.domain.MemberExtInfo;
import com.ruoyi.db.mapper.AddressInfoMapper;
import com.ruoyi.db.mapper.MemberExtInfoMapper;
import com.ruoyi.db.model.param.MemberExtInfoParam;
import com.ruoyi.db.model.vo.DataInfoVO;
import com.ruoyi.member.constant.CityInfoConstant;
import com.ruoyi.member.model.MapProvinceCityVO;
import com.ruoyi.member.service.IMemberExtInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户扩展信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MemberExtInfoServiceImpl implements IMemberExtInfoService 
{
    @Resource
    private MemberExtInfoMapper memberExtInfoMapper;

    /**
     * 查询用户扩展信息
     * 
     * @param uid 用户扩展信息主键
     * @return 用户扩展信息
     */
    @Override
    public MemberExtInfo selectMemberExtInfoByUid(Long uid)
    {
        return memberExtInfoMapper.selectMemberExtInfoByUid(uid);
    }

    /**
     * 查询用户扩展信息列表
     * 
     * @param memberExtInfo 用户扩展信息
     * @return 用户扩展信息
     */
    @Override
    public List<MemberExtInfo> selectMemberExtInfoList(MemberExtInfo memberExtInfo)
    {
        return memberExtInfoMapper.selectMemberExtInfoList(memberExtInfo);
    }

    /**
     * 查询用户扩展信息列表
     *
     * @param memberExtInfo 用户扩展信息
     * @return 用户扩展信息
     */
    @Override
    public List<Map<String, Object>> selectMemberExtInfoListMap(MemberExtInfoParam memberExtInfo)
    {
        return memberExtInfoMapper.selectMemberExtInfoListMap(memberExtInfo);
    }

    /**
     * 新增用户扩展信息
     * 
     * @param memberExtInfo 用户扩展信息
     * @return 结果
     */
    @Override
    public int insertMemberExtInfo(MemberExtInfo memberExtInfo)
    {
        memberExtInfo.setCreateTime(DateUtils.getNowDate());
        return memberExtInfoMapper.insertMemberExtInfo(memberExtInfo);
    }

    /**
     * 修改用户扩展信息
     * 
     * @param memberExtInfo 用户扩展信息
     * @return 结果
     */
    @Override
    public int updateMemberExtInfo(MemberExtInfo memberExtInfo)
    {
        memberExtInfo.setUpdateTime(DateUtils.getNowDate());
        return memberExtInfoMapper.updateMemberExtInfo(memberExtInfo);
    }

    /**
     * 批量删除用户扩展信息
     * 
     * @param uids 需要删除的用户扩展信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberExtInfoByUids(String uids)
    {
        return memberExtInfoMapper.deleteMemberExtInfoByUids(Convert.toStrArray(uids));
    }

    @Override
    public List<DataInfoVO> getZhimaCountInfoByDateInfoAndChannelCode(String dateInfo, String channelCode) {
        return memberExtInfoMapper.getZhimaCountInfoByDateInfoAndChannelCode(dateInfo, channelCode);
    }

    @Override
    public List<DataInfoVO> getAuthCountInfoByDateInfoAndChannelCode(String dateInfo, String channelCode) {
        return memberExtInfoMapper.getAuthCountInfoByDateInfoAndChannelCode(dateInfo, channelCode);
    }

    @Override
    public DataInfoVO getAgeCountInfoByDateInfoAndChannelCode(String dateInfo, String channelCode, Integer min, Integer max) {
        return memberExtInfoMapper.getAgeCountInfoByDateInfoAndChannelCode(dateInfo, channelCode, min, max);
    }

    @Override
    public List<Map<String, Object>> getCityCountInfoByDateInfoAndChannelCode(String dateInfo, String channelCode) {
        return memberExtInfoMapper.getCityCountInfoByDateInfoAndChannelCode(dateInfo, channelCode);
    }

    @Override
    public Long getCountNumberByCountInfo(String dateInfo, String channelCode) {
        return memberExtInfoMapper.getCountNumberByCountInfo(dateInfo, channelCode);
    }

    @Override
    public JSONObject getMapProvinceDataList(String startDate, String endDate){

        Long authNumber = memberExtInfoMapper.countAuthNumberByDate(startDate, endDate);

        List<Map<String, Object>> resultList = memberExtInfoMapper.countProvinceAuthNumberByDate(startDate, endDate);

        final Long[] maxValue = {0L};
        List<MapProvinceCityVO> mapProvinceList = new ArrayList<>();
        resultList.stream().forEach(map -> {

            String code = (String) map.get("code");
            Long number = (Long) map.get("number");

            if (number > maxValue[0]){
                maxValue[0] = number;
            }

            MapProvinceCityVO provinceCityVO = CityInfoConstant.getMapProvinceInfoByCode(code);

            provinceCityVO.setNumber(String.valueOf(number));
            provinceCityVO.setPercent(NumberUtil.div(new BigDecimal(number), new BigDecimal(authNumber), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
            provinceCityVO.setValue(new BigDecimal(number));

            mapProvinceList.add(provinceCityVO);
        });

        JSONObject resultData = new JSONObject();
        resultData.put("maxValue", maxValue[0]);
        resultData.put("startDate", startDate);
        resultData.put("endDate", endDate);
        resultData.put("totalNumber", authNumber);
        resultData.put("dataList", mapProvinceList);
        resultData.put("queryDateInfo", StrUtil.format("{} ~ {}", startDate, endDate));

        return resultData;
    }

    @Override
    public JSONObject getMapCityDataList(String startDate, String endDate, String provinceCode) {

        List<Map<String, Object>> cityMapList = memberExtInfoMapper.getAuthInfoByDateAndProvince(startDate, endDate, provinceCode);

        List<MapProvinceCityVO> mapProvinceList = new ArrayList<>();
        cityMapList.stream().forEach(map -> {

            String name = (String) map.get("name");
            Long number = (Long) map.get("number");
            String code = (String) map.get("code");

            MapProvinceCityVO cityVO = new MapProvinceCityVO();
            cityVO.setName(StrUtil.format("{}/{}",name, number) );
            cityVO.setNumber(String.valueOf(number));
            cityVO.setCode(code);
            cityVO.setValue(new BigDecimal(number));

            mapProvinceList.add(cityVO);
        });

        Long aLong = memberExtInfoMapper.countAuthNumberByDateAndProvince(startDate, endDate, provinceCode);

        AddressInfo addressInfo = addressInfoMapper.selectAddressInfoByCodeValue(provinceCode);

        JSONObject resultData = new JSONObject();
        resultData.put("totalNumber", aLong);
        resultData.put("name", addressInfo.getName());
        resultData.put("dataList", mapProvinceList);

        return resultData;
    }

    @Override
    public JSONObject chartMap100CityData(String startDate, String endDate) {

        List<Map<String, Object>> cityMapList = memberExtInfoMapper.getAuthInfo100City(startDate, endDate);

        JSONArray dataList = new JSONArray();
        cityMapList.stream().forEach(map -> {

            String name = (String) map.get("name");
            Long number = (Long) map.get("number");

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", StrUtil.format("{}/{}", name , number));
            jsonObject.put("value", number);

            dataList.add(jsonObject);
        });

        JSONObject resultData = new JSONObject();
        resultData.put("dataList", dataList);
        resultData.put("queryDateInfo", StrUtil.format("{} ~ {}", startDate, endDate));

        return resultData;
    }

    @Override
    public Long selectCountByDatetime(String starttime, String endtime) {
        return memberExtInfoMapper.getCountNumberByDatetime(starttime, endtime);
    }

    @Resource
    private AddressInfoMapper addressInfoMapper;
}
